<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cate;

class CateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = [];
        
        $list = Cate::where($where)->paginate(10);        
        return view('admin.cate.index',compact('list'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->only(['name']);
        $flag = Cate::create($data);
        if($flag) {
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Cate::find($id);
        return view('admin.cate.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $rs['status'] = 'danger';
        $data = $request->except('_token','_method');
        //dd($data);
        if ($request->file('thumb')) {
            $data['thumb'] = '/'.$request->thumb->store('uploads/'.date('Ymd'));     
        }
        $flag = Cate::where(['id'=>$id])->update($data);
        if($flag) {
            $rs['msg']    = '操作成功';
            $rs['status'] = 'success';
            return back()->with('rs',$rs);    
        }
        $rs['msg'] = '操作失败';
        return redirect('zadmin/cate')->with('rs',$rs);
        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Cate::destroy($id);
        return back();
    }
}
